package com.mc.code.web.sys;

import com.mc.code.entity.sys.SysMessageManage;
import com.mc.code.service.LoginService;
import com.mc.code.sysService.SysConsoleService;
import com.mc.code.sysService.SysMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
public class SystemManageController {

    //分页
    private static final long serialVersionUID = 1L;
    private static final int PAGE_COUNT = 3;//每页显示记录数

    @Autowired
    private SysMessageService sysMessageService;
    @Autowired
    private LoginService loginService;//前台登录
    @Autowired
    private SysConsoleService sysConsoleService;

    //控制台
    @RequestMapping("/console")
    public String consoleManage(Model model){
        //用户总数
        model.addAttribute("userCount",sysConsoleService.codeUserCount());
        //项目总数
        model.addAttribute("itemsCount",sysConsoleService.itemsCount());
        //开发者总数
        model.addAttribute("developerCount",sysConsoleService.developerCount());
        //需求者总数
        model.addAttribute("neederCount",sysConsoleService.neederCount());
        //项目类型
        model.addAttribute("web",sysConsoleService.webCount());
        model.addAttribute("app",sysConsoleService.appCount());
        model.addAttribute("weChart",sysConsoleService.weChart());
        model.addAttribute("smallPrograme",sysConsoleService.smallPrograme());
        model.addAttribute("htmlCount",sysConsoleService.htmlCount());
        model.addAttribute("othersCount",sysConsoleService.othersCount());
        //开发人员擅长的技术方向
        model.addAttribute("netS",loginService.skillNet());
        model.addAttribute("javaS",loginService.skillJava());
        model.addAttribute("phpS",loginService.skillPhp());
        model.addAttribute("aspS",loginService.skillAsp());
        model.addAttribute("cS",loginService.skillC());
        model.addAttribute("bigdataS",loginService.skillBigdata());
        return "sys/console";
    }

//    //留言列表
//    @RequestMapping("/messageList")
//    public String messageList(Model model){
//        //展示所有的留言
//        List<SysMessageManage> sysMessageManageList = sysMessageService.showAllMessage();
//        model.addAttribute("messageList",sysMessageManageList);
//        return "sys/messageList";
//    }
    //对留言进行筛选查找 + 分页
    @RequestMapping("/messageList")
    public String findMessageByCondition(Model model, String messageState, HttpServletRequest request){
        //默认当前页
        int currentPage = 1;
        String page = request.getParameter("page");
        if(page != null) {
            currentPage = Integer.valueOf(page);
        }
        List<SysMessageManage> sysMessageManageList = sysMessageService.findMessageByCondition(messageState,currentPage,PAGE_COUNT);
        int totalPage = sysMessageService.getTotalPage(PAGE_COUNT,messageState);
        model.addAttribute("totalPage", totalPage);
        model.addAttribute("currentPage", currentPage);
        model.addAttribute("messageList",sysMessageManageList);
        model.addAttribute("messageState",messageState);
        return "sys/messageList";
    }

    //查看留言
    @RequestMapping("/readMessage")
    public String readMessage(int id,Model model){
        //当点击查看时，应将状态改为已读
        sysMessageService.updateState(id);
        SysMessageManage sysMessageManage = sysMessageService.findMessageById(id);
        //定义一个时间格式的模板
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //将数据库中的创建时间格式化 date转string
        String createTime = dateFormat.format(sysMessageManage.getCreateTime());
        model.addAttribute("createTime",createTime);
        //回复时间格式化date转string
        String replyTime = dateFormat.format(new Date());
        model.addAttribute("replyTime",replyTime);
        model.addAttribute("sysMessage",sysMessageManage);
        return "sys/replyMessage";
    }
    //回复留言
    @RequestMapping("/replyMessage")
    public String replyMessage(SysMessageManage sysMessageManage, HttpServletRequest request){
        //接受前台传入的string型的日期
        String replyTime = request.getParameter("replytime");
        String createTime = request.getParameter("createtime");
        //将时间类型由string转为date型
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            sysMessageManage.setCreateTime(dateFormat.parse(createTime));
            sysMessageManage.setReplyTime(dateFormat.parse(replyTime));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        System.out.println("回复后的："+sysMessageManage);
        //将回复的信息修改到数据库中
        sysMessageService.replyMessage(sysMessageManage);
        return "redirect:messageList";
    }

    //通知列表
    @RequestMapping("/noticeList")
    public String noticeList(){
        return "sys/noticeManage";
    }

    //添加通知
    @RequestMapping("/addNotice")
    public String addNotice(){
        return "sys/addNotice";
    }
}
